package com.xiaoyu.lanling.feature.checkin

import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.BaseBottomSheetDialogFragment
import com.xiaoyu.lanling.activity.base.BaseDialogFragment
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.feature.checkin.datamodel.CheckInSuccessModel
import com.xiaoyu.lanling.feature.checkin.log.CheckInLogger
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import kotlinx.android.synthetic.main.check_in_success_dialog.*

class CheckInSuccessDialogFragment : BaseDialogFragment() {

    companion object {
        val TAG: String = CheckInSuccessDialogFragment::class.java.simpleName

        fun show(fragmentManager: FragmentManager, model: CheckInSuccessModel, dismissListener: OnDismissListener) {
            val params = Bundle()
            params.putSerializable(Constant.IntentKey.KEY_CHECK_IN_MODEL, model)

            val fragment = CheckInSuccessDialogFragment()
            fragment.arguments = params
            fragment.setOnDismissListener(dismissListener)
            fragment.show(fragmentManager, TAG)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFullScreenDimStyle)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return layoutInflater.inflate(R.layout.check_in_success_dialog, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)
        initView()
        initBind()
    }

    private fun initView() {
        val model = arguments?.getSerializable(Constant.IntentKey.KEY_CHECK_IN_MODEL) as? CheckInSuccessModel
                ?: return
        check_in_days_desc.text = getCheckInDaysDesc(model.continuousSignInDays)
        ImageViewUtil.loadImage(icon, model.imageLoadParam)
        amount_desc.text = getString(R.string.check_in_success_dialog_amount_desc, model.amount)
    }

    private fun initBind() {
        view?.setOnClickDebounceListener { dismiss() }
        close?.setOnClickDebounceListener { dismiss() }
        mission_button?.setOnClickDebounceListener {
            activity?.let {
                CheckInLogger.checkInMissionClick()
                Router.instance.gotoMissionListActivity(it)
                dismiss()
            }
        }
    }

    private fun getCheckInDaysDesc(days: Int): SpannableString {
        val daysString = days.toString()
        val title = SpannableString(getString(R.string.check_in_days_desc, daysString))
        val start = title.indexOf(daysString)
        val end = start + daysString.length
        title.setSpan(ForegroundColorSpan(AppContext.getColor(R.color.colorOnSecondary)), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        title.setSpan(RelativeSizeSpan(1.7F), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        return title
    }
}